import 'package:easy_refresh/easy_refresh.dart';
import 'package:f200/common/index.dart';
import 'package:f200/features/categroy/data/category_repository.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../domain/category_model.dart';

class CategoryScreenController extends GetxController {
  final ScrollController outerScrollController = ScrollController();
  var leftBarWidth = 90.0;
  RxInt selectedCategoryId = 0.obs;
  // GoodsCategoryModel? currentCategory;
  Rx<GoodsCategoryModel> currentCategory =
      GoodsCategoryModel(id: 0, name: '').obs;
  late EasyRefreshController refreshController;

  get category => currentCategory.value;

  @override
  void onInit() {
    super.onInit();
    //初始化滚动控制器
    refreshController = EasyRefreshController(
      controlFinishLoad: true,
      controlFinishRefresh: true,
    );
    // 初始化选中的分类
    selectedCategoryId.value = 0;
    onTapCategory(selectedCategoryId.value);
  }

  void onTapCategory(int index) {
    selectedCategoryId.value = index;
    currentCategory.update((val) {
      var v = _fetchCategoryInfoById(index);
      val?.name = v.name;
      val?.desc = v.desc;
    });
    logD('category index:   ' + selectedCategoryId.value.toString());
    logD(currentCategory.value.toString());
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    outerScrollController.dispose();
    refreshController.dispose();
  }

  GoodsCategoryModel _fetchCategoryInfoById(int index) {
    return categories[index];
  }
}
